package com.pp.ent.base.concurrent;

import android.os.Looper;

import com.pp.ent.Cherry;
import com.pp.ent.base.log.MLog;

import java.util.concurrent.Callable;

/**
 * Created by dengqu on 2016/11/11.
 */

public abstract class XLCommand<T> implements Callable<XLCommandResult> {
    private final static String TAG = XLCommand.class.getSimpleName();
    private XLCommandResult<T> mCmmandResult;
    private XLCommandListener mXlCommandResponse;

    @Override
    public XLCommandResult call() throws Exception {
        MLog.debug(TAG, "Looper.getMainLooper()==Looper.myLooper() " + (Looper.getMainLooper() == Looper.myLooper() ? "true" : "false"));
        mCmmandResult = new XLCommandResult<>();
        try {
            mCmmandResult.data = excute();
        } catch (Exception e) {
            MLog.error(TAG, e);
            mCmmandResult.resultCode = XLCommandResult.ResultCode.FAILED;
        } finally {
            Cherry.notifyUI(this);
            return mCmmandResult;
        }
    }

    protected abstract T excute() throws Exception;


    public void onResponse() {
        if (mXlCommandResponse != null) {
            mXlCommandResponse.onResponse(mCmmandResult);
        }
    }

    public void setXlCommandResponse(XLCommandListener mXlCommandResponse) {
        this.mXlCommandResponse = mXlCommandResponse;
    }
}
